namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Definitions;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.IO;
    using System.Xml;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The PaDnnAdapter_V2Skin extends PaDnnAdapterBase to support V2 Skin Object
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	04/21/2005  documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class PaDnnAdapter_V2Skin : PaDnnAdapterBase
    {
        public PaDnnAdapter_V2Skin(PaInstallInfo InstallerInfo) : base(InstallerInfo)
        {
        }

        public override PaFolderCollection ReadDnn()
        {
            IEnumerator refObjectHelperL4=null;
            this.InstallerInfo.Log.StartJob(base.DNN_Reading);
            ArrayList DnnErrors = this.ValidateDnn();
            if (DnnErrors.Count == 0)
            {
                IEnumerator refObjectHelperL0=null;
                this.InstallerInfo.Log.AddInfo(base.DNN_ValidSkinObject);
                PaFolderCollection Folders = new PaFolderCollection();
                XmlDocument doc = new XmlDocument();
                MemoryStream buffer = new MemoryStream(this.InstallerInfo.DnnFile.Buffer, false);
                doc.Load(buffer);
                this.InstallerInfo.Log.AddInfo(base.XML_Loaded);
                XmlNode dnnRoot = doc.DocumentElement;
                int TempModuleDefinitionID = 0;
                try
                {
                    refObjectHelperL0 = dnnRoot.SelectNodes("folders/folder").GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        IEnumerator refObjectHelperL1=null;
                        IEnumerator refObjectHelperL2 = null;
                        XmlElement FolderElement = (XmlElement) refObjectHelperL0.Current;
                        PaFolder Folder = new PaFolder();
                        try
                        {
                            Folder.Name = FolderElement.SelectSingleNode("name").InnerText.Trim();
                        }
                        catch (Exception exception1)
                        {
                            
                            Exception ex = exception1;
                            throw new Exception(base.EXCEPTION_FolderName);
                            
                        }
                        Folder.FriendlyName = Folder.Name;
                        Folder.FolderName = Folder.Name;
                        Folder.ModuleName = Folder.Name;
                        XmlElement resourcefileElement = (XmlElement) FolderElement.SelectSingleNode("resourcefile");
                        if (resourcefileElement != null)
                        {
                            Folder.ResourceFile = resourcefileElement.InnerText.Trim();
                            PaFile paRF = (PaFile) this.InstallerInfo.FileTable[Folder.ResourceFile.ToLower()];
                            if (paRF == null)
                            {
                                this.InstallerInfo.Log.AddFailure(base.EXCEPTION_MissingResource + Folder.ResourceFile);
                            }
                            else
                            {
                                paRF.Path = Folder.ResourceFile;
                            }
                        }
                        this.InstallerInfo.Log.AddInfo(base.FILES_Loading);
                        try
                        {
                            refObjectHelperL1 = FolderElement.SelectNodes("files/file").GetEnumerator();
                            while (refObjectHelperL1.MoveNext())
                            {
                                string name;
                                XmlElement file = (XmlElement) refObjectHelperL1.Current;
                                try
                                {
                                    name = file.SelectSingleNode("name").InnerText.Trim();
                                }
                                catch (Exception exception2)
                                {
                                    ProjectData.SetProjectError(exception2);
                                    Exception ex = exception2;
                                    throw new Exception(base.EXCEPTION_FileName);
                                    
                                }
                                PaFile paf = (PaFile) this.InstallerInfo.FileTable[name.ToLower()];
                                if (paf == null)
                                {
                                    this.InstallerInfo.Log.AddFailure(base.FILE_NotFound + name);
                                }
                                else
                                {
                                    XmlElement pathElement = (XmlElement) file.SelectSingleNode("path");
                                    if (pathElement != null)
                                    {
                                        string filepath = pathElement.InnerText.Trim();
                                        this.InstallerInfo.Log.AddInfo(string.Format(base.FILE_Found, filepath, name));
                                        paf.Path = filepath;
                                    }
                                    Folder.Files.Add(paf);
                                }
                            }
                        }
                        finally
                        {
                            if (refObjectHelperL1 is IDisposable)
                            {
                                (refObjectHelperL1 as IDisposable).Dispose();
                            }
                        }
                        if (!Folder.Files.Contains(this.InstallerInfo.DnnFile))
                        {
                            Folder.Files.Add(this.InstallerInfo.DnnFile);
                        }
                        this.InstallerInfo.Log.AddInfo(base.MODULES_Loading);
                        try
                        {
                            refObjectHelperL2 = FolderElement.SelectNodes("modules/module").GetEnumerator();
                            while (refObjectHelperL2.MoveNext())
                            {
                                IEnumerator refObjectHelperL3=null;
                                XmlElement DNNModule = (XmlElement) refObjectHelperL2.Current;
                                ModuleDefinitionInfo ModuleDef = new ModuleDefinitionInfo();
                                ModuleDef.TempModuleID = TempModuleDefinitionID;
                                TempModuleDefinitionID++;
                                Folder.Modules.Add(ModuleDef);
                                this.InstallerInfo.Log.AddInfo(string.Format(base.MODULES_ControlInfo, ModuleDef.FriendlyName));
                                try
                                {
                                    refObjectHelperL3 = DNNModule.SelectNodes("controls/control").GetEnumerator();
                                    while (refObjectHelperL3.MoveNext())
                                    {
                                        XmlElement ModuleControl = (XmlElement) refObjectHelperL3.Current;
                                        ModuleControlInfo ModuleControlDef = new ModuleControlInfo();
                                        XmlElement keyElement = (XmlElement) ModuleControl.SelectSingleNode("key");
                                        if (keyElement != null)
                                        {
                                            ModuleControlDef.ControlKey = keyElement.InnerText.Trim();
                                        }
                                        XmlElement titleElement = (XmlElement) ModuleControl.SelectSingleNode("title");
                                        if (titleElement != null)
                                        {
                                            ModuleControlDef.ControlTitle = titleElement.InnerText.Trim();
                                        }
                                        try
                                        {
                                            ModuleControlDef.ControlSrc = Path.Combine("DesktopModules", Path.Combine(Folder.Name, ModuleControl.SelectSingleNode("src").InnerText.Trim()).Replace('\\', '/')).Replace(@"\", "/");
                                        }
                                        catch (Exception exception3)
                                        {
                                            ProjectData.SetProjectError(exception3);
                                            Exception ex = exception3;
                                            throw new Exception(base.EXCEPTION_Src);
                                            
                                        }
                                        XmlElement iconFileElement = (XmlElement) ModuleControl.SelectSingleNode("iconfile");
                                        if (iconFileElement != null)
                                        {
                                            ModuleControlDef.IconFile = iconFileElement.InnerText.Trim();
                                        }
                                        try
                                        {
                                            ModuleControlDef.ControlType = (SecurityAccessLevel) Conversions.ToInteger(TypeDescriptor.GetConverter(typeof(SecurityAccessLevel)).ConvertFromString(ModuleControl.SelectSingleNode("type").InnerText.Trim()));
                                        }
                                        catch (Exception exception4)
                                        {
                                            ProjectData.SetProjectError(exception4);
                                            Exception ex = exception4;
                                            throw new Exception(base.EXCEPTION_Type);
                                            
                                        }
                                        XmlElement orderElement = (XmlElement) ModuleControl.SelectSingleNode("vieworder");
                                        if (orderElement != null)
                                        {
                                            ModuleControlDef.ViewOrder = Conversions.ToInteger(orderElement.InnerText.Trim());
                                        }
                                        else
                                        {
                                            ModuleControlDef.ViewOrder = Null.NullInteger;
                                        }
                                        ModuleControlDef.ModuleDefID = ModuleDef.TempModuleID;
                                        Folder.Controls.Add(ModuleControlDef);
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL3 is IDisposable)
                                    {
                                        (refObjectHelperL3 as IDisposable).Dispose();
                                    }
                                }
                            }
                        }
                        finally
                        {
                            if (refObjectHelperL2 is IDisposable)
                            {
                                (refObjectHelperL2 as IDisposable).Dispose();
                            }
                        }
                        Folders.Add(Folder);
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                if (!this.InstallerInfo.Log.Valid)
                {
                    throw new Exception(base.EXCEPTION_LoadFailed);
                }
                this.InstallerInfo.Log.EndJob(base.DNN_Success);
                return Folders;
            }
            try
            {
                refObjectHelperL4 = DnnErrors.GetEnumerator();
                while (refObjectHelperL4.MoveNext())
                {
                    string err = Conversions.ToString(refObjectHelperL4.Current);
                    this.InstallerInfo.Log.AddFailure(err);
                }
            }
            finally
            {
                if (refObjectHelperL4 is IDisposable)
                {
                    (refObjectHelperL4 as IDisposable).Dispose();
                }
            }
            throw new Exception(base.EXCEPTION_Format);
        }

        private ArrayList ValidateDnn()
        {
            ModuleDefinitionValidator ModuleValidator = new ModuleDefinitionValidator();
            MemoryStream xmlStream = new MemoryStream(this.InstallerInfo.DnnFile.Buffer);
            ModuleValidator.Validate(xmlStream);
            return ModuleValidator.Errors;
        }
    }
}

